package controllers.portal;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import rbac.models.Oplog;
import models.Info;
import models.Infotype;
import rbac.models.User;
import utils.Md5Util;

import com.et.mvc.JsonView;
import com.et.mvc.View;

import controllers.ApplicationController;

public class InfoController extends ApplicationController {
	public void index() throws Exception {
	}
	
	public View getInfos(int page, int rows, int InfoTypeId, String sort, String order)
			throws Exception {
		String orderBy = "editdate desc";
		if (sort != null && order != null) {
			orderBy = sort + " " + order;
		}

		String cond = null;
		Object[] args = null;
		if (InfoTypeId > 0) {
			cond = "Infotypeid=?";
			args = new Object[] { InfoTypeId };
		}
		long total = Info.count(Info.class, cond, args);
		List<Info> items = Info.findAll(Info.class, cond, args,
				orderBy, rows, (page - 1) * rows);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("total", total);
		result.put("rows", items);
		return new JsonView(result);
	}

	public View getInfoTree() throws Exception {
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		List<Infotype> types = Infotype.findAll(Infotype.class,
				null, null, "sort");
		for (Infotype type : types) {
			Map<String, Object> group = new HashMap<String, Object>();
			group.put("id", "group-" + type.getId());
			group.put("text", type.getName());

			
			result.add(group);
		}
		return new JsonView(result.toArray(new Object[result.size()]));
	}

	public View save(Info item) throws Exception {				
		User user = (User) rbac.getCurrentUser();				
		String content = Md5Util.getBASE64(item.getContent());
		
		item.setContent(content);
		item.setHot(0);
		item.setOper(user.getLogin());
		item.setInputdate(new java.sql.Timestamp(System
				.currentTimeMillis()));
		item.setEditdate(new java.sql.Timestamp(System
				.currentTimeMillis()));
		item.save();
		Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")新建资讯，内容为：" + new JsonView(item));
		
		View view = new JsonView("success:true");
		view.setContentType("text/plain;charset=utf-8");
		return view;
	}

	public View update(int id, Map<String, Object> params) throws Exception {
		User user = (User) rbac.getCurrentUser();	
		Info item = Info.find(Info.class, id);
		JsonView olditem= new JsonView(item);
		item = Info.updateModel(item, params);
		String content = Md5Util.getBASE64(item.getContent());
		
		item.setContent(content);
		item.setOper(user.getLogin());
		item.setInputdate(new java.sql.Timestamp(System
				.currentTimeMillis()));
		item.setEditdate(new java.sql.Timestamp(System
				.currentTimeMillis()));
		item.save();
					
		Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")修改资讯，原内容为："+ olditem + "\r\n修改内容为：" + new JsonView(item));										

		View view = new JsonView("success:true");
		view.setContentType("text/plain;charset=utf-8");
		return view;
	}

	public View destroy(int id) throws Exception {
		Info item = Info.find(Info.class, id);
		User user = (User) rbac.getCurrentUser();				
		Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")删除资讯，内容为：" + new JsonView(item));									

		item.destroy();
		return new JsonView("success:true");
	}

	
}
